home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d12
/
ddj0190.arc
/
RAHNER.EXE
/
EGA_DRV.ASM
< prev
next >
Wrap
Assembly Source File
|
1989-09-21
|
20KB
|
786 lines
.model small, c
.286 ; This directive can be used to optimize procedure entry
; but not much else. I avoided all non-8088 commands
comment \
EGA Sprite Drivers for C
Copyright (c) February 1989, Ryu Consulting, Inc.
(916) 722 - 1939 anytime
Written by Rahner James, CS
This is a full functioning sprite driver for EGA graphics
adaptors. The sprite are given to the routines as a linked list
of sprite structures. All the function are re-entrant and can be
part of a multi-tasking system. The sprite structures are intended
to reside in far memory. The sprite can exist on any pixel boundary.
These were intended to be called from some C program, but probably
can be modified for some other language. This must be assembled with
Microsoft MASM version 5.0 or later since I make use of local
variables, forward/backward jumps and models. Expect to get some
incorrect size warnings because MASM doesn't seem to recognize its own
"byte ptr" and "word ptr" operators when used with words and dwords.
\
; ****************************************************************************
; EQUATES
; ****************************************************************************
EOI equ 20h ; End Of Interrupt signal
EOI_PORT equ 20h ; Port to output the EOI
CRT_MODE equ 49h
EGA_ADDRESS equ 63h
EGA_PIXELS_WORD equ 16 ; Number of pixels per word
EGA_PIXELS_BYTE equ 8 ; Number of pixels per byte
NUMBER_OF_PLANES equ 4 ; Number of EGA color planes
EGA_RETRACE_STATUS equ 3dah ; EGA retrace status register
RETRACE_BIT equ 1 shl 3 ; Bit set to signal a vertical retrace
SEQUENCE_REG equ 3c4h ; Sequencer register
GRAPHICS_12 equ 3ceh ; Graphics 1 & 2 register
MAP_MASK_REG equ 2 ; Map mask Indexed register
DATA_ROTATE_REG equ 3 ; Data Rotate Indexed register
DATA_OR equ 1 shl 4 ; Set to OR data on the EGA
DATA_MOVE equ 0 ; Write data unmodified onto EGA
BOTTOM_LINE equ 200 ; Lowest pixel line to allow a sprite
RIGHT_SIDE equ 640 ; Right-most visual pixel allowed
; ****************************************************************************
; STRUCTURES
; ****************************************************************************
; All sprites are stored on disk using the same internal format.
; The first word is the width of the sprites in bytes and the second
; word is the height of the sprite in widths. Each byte represents
; one pixel's worth of information. Bit 7 of the byte is the intensity
; bit, 0=off. This allows for 128 colors, black will be 0 or 80h. The
; intensity bit set indicates an opaque black surface. All color
; translations are table driven.
internal_sprite_structure struc ; Storage structure used for all sprites
int_width dw ? ; Width in bytes for the sprite
int_height dw ? ; Height in widths of the sprite
int_body db ? ; Start of the sprite's body
internal_sprite_structure ends
ega_sprite_structure struc ; Internal EGA sprite structure
e_animate_ptr dw 0,0 ; Far ptr to next sprite struct in animation seq.
e_width dw ? ; Width of the sprite in words
e_height dw ? ; Height of sprite in widths
e_body dw ? ; Beginning of body
;.word 0: mask, word 1: sprite
;.throughout the body. That way you can pull
;.the background up, mask it, OR the sprite,
;.then store the background
; The body is organized into four planes,
;.termed PLANE0 to PLANE3. Each represents a
;.different color in the EGA spectrum, except
;.PLANE0 which is the intensity bit.
ega_sprite_structure ends
style_structure struc
style_width dw ? ; Width of each style entry in bytes
style_height dw ? ; Height of each style entry in pixels
style_body db ? ; Start of the style entries
style_structure ends
; ****************************************************************************
; LOCAL DATA STORAGE for DS
; ****************************************************************************
.data
public do_page_flip, done_page_flip
even
do_page_flip db 0 ; Set to -1 when non-visual page is completed
done_page_flip db -1 ; Set to -1 right after page has been swapped
flip_turn db 4 ; # of interrupts before an EGA page flip
old_irq_mask db 0 ; Old IRQ mask
EGA_settings db 2bh,2bh,2bh,2bh,24h,24h,23h,2eh
db 0,0,0,0,0,24h,23h,2eh,2bh
even
ega_base_port dw ?
default_retrace label word
v_retrace_reg db 11h
v_retrace_value db ?
.code
; ****************************************************************************
; LOCAL DATA STORAGE for CS
; ****************************************************************************
even
ega_segment dw 0a800h ; EGA page memory segment being set up
old_vector dw 0,0 ; Old IRQ-2 vector (as Checkov would say wecter)
; EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
; EGA sprite related routines below this line
; void EGA_CONVERT( dest_ptr, source_ptr )
; Converts long storage format sprite into EGA structure sprite
; Given:
; dest_ptr far -> EGA sprite buffer ready to go
; source_ptr far -> disk sprite structure to convert
; Returns:
; EGA sprite buffer set up accordingly
ega_convert proc near uses di si ds, dest:dword, source:dword
local store_width:word, store_height:word
local source_width:word
local plane_size:word
cld
lds si, source ; DS:SI -> source sprite
les di, dest ; ES:DI -> CGA sprite buffer
mov es:[di], di ; Make the animate ptr point to itself
mov es:[di+2], es
add di, E_WIDTH
lodsw ; Get width in byte pixels
mov source_width, ax
add ax, EGA_PIXELS_WORD-1 ; Want to include those border pixels
mov cl, 4 ; Divide by sixteen to convert one byte
shr ax, cl ;.per pixel to 16 pixels per word for EGA
mov bx, ax ; Use this as our width count
stosw ; Store as words/pixels
lodsw ; Get height
mov store_height, ax
stosw ; Move height straight across
mul bx ; AX = Body size in mask/sprite entries
add ax, ax ; AX = body size in words
add ax, ax ; AX = plane size in bytes
mov plane_size, ax ; Save as our plane index
mov ax, ds ; Swap DS:SI and ES:DI
mov bx, es
mov ds, bx
mov es, ax
xchg di, si
sub si, 4 ; This is to prep for the next INC
next_row:
mov cx, source_width ; Get source row width
next_word:
add si, 4 ; SI -> next word in line
mov dx, -1 ; DX = the destination mask
xor ax, ax
mov [si], dx ; Set the mask word
mov [si+2], ax ; Clear the sprite word
mov bx, plane_size ; BX offset to next plane
mov [si+bx], dx ; Set the mask word
mov [si+bx+2], ax ; Clear the sprite word
add bx, plane_size ; BX offset to next plane
mov [si+bx], dx ; Set the mask word
mov [si+bx+2], ax ; Clear the sprite word
add bx, plane_size ; BX offset to next plane
mov [si+bx], dx ; Set the mask word
mov [si+bx+2], ax ; Clear the sprite word
mov dx, 1 shl 7 ; Start at MSB which is pixel LSB
next_pixel_byte:
jc next_word ; Only be set by pixel shift below
mov al, es:[di] ; Get the source pixel byte
inc di ; DI -> next source pixel byte
or al, al ; See if it's anything at all
jz end_pixel_byte ; Skip all the checks
xor [si], dx ; Reset the mask bit
test al, 1 shl 4 ; Check bit 7
jz @F ; Skip if nothing here
or [si+2], dx ; Place the sprite bit
@@: mov bx, plane_size ; BX -> plane 1 offset
xor [si+bx], dx ; Clear the mask bit
test al, 1 shl 7 ; Check bit 6
jz @F ; Skip if nothing here
or [si+bx+2], dx ; Place the sprite bit
@@: add bx, plane_size ; BX -> plane 2 offset
xor [si+bx], dx ; Clear the mask bit
test al, 1 shl 6 ; Check bit 5
jz @F ; Skip if nothing here
or [si+bx+2], dx ; Place the sprite bit
@@: add bx, plane_size ; BX -> plane 3 offset
xor [si+bx], dx ; Clear the mask bit
test al, 1 shl 5 ; Check bit 4
jz end_pixel_byte ; Skip if nothing here
or [si+bx+2], dx ; Place the sprite bit
end_pixel_byte:
shr dl, 1 ; Move our pixel bit toward MS pixel bit
rcr dh, 1
loop next_pixel_byte ; Loop through the pixel bytes
dec store_height ; One less row
jnz next_row
ret
ega_convert endp
; EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
;
; ui EGA_CALCULATE( source_ptr )
; Calculates the amount of storage needed for an unconverted sprite
; Given:
; source_ptr far -> disk sprite structure to convert
; Returns:
; AX = number of bytes needed to store the converted sprite
ega_calculate proc near uses si ds, source:dword
lds si, source ; DS:SI -> disk sprite structure for calculation
mov ax, [si] ; Get the width in bytes
add ax, EGA_PIXELS_WORD-1 ; Round up to nearest word
shr ax, 4 ; AX = number of words for stoarge
add ax, ax ; AX = number of bytes storage
add ax, ax ; AX = number of globs in one row
add ax, ax ; AX = number of row/planes
add ax, ax
mul word ptr [si].int_height ; AX = bytes per row * number of rows
add ax, E_BODY ; AX = body size + header size
ret
ega_calculate endp
; EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
;
; void PUT_SPRITE( ui X, ui Y, ega_sprite_structure far *SPRITE_PTR )
; Maps a sprite list into the display video buffer
; Sprite is assumed to be 32-bits wide
; Given:
; X = X pixel location of sprite
; Y = Y pixel location of sprite
; SPRITE_PTR -> sprite structure to put on the screen
; Returns:
; The sprite is mapped onto the screen buffer
put_sprite proc uses di si, x:word, y:word, sprite_ptr:dword
local mask_msw:word ; OR'ed with the mask dword
local mask_lsw:word
local or_msw:word ; AND'ed with the sprite dword
local or_lsw:word
local start_di:word
local start_si:word
local sprite_plane_size:word
local number_of_rows:word
local shift:byte
local plane_number:byte
push ds
cmp y, 200 ; See if below 200
jc @F
short_done:
jmp done
@@: cmp x, 640
jnc short_done
cld
mov es, ega_segment
lds si, sprite_ptr ; DS:SI -> sprite to be driven
mov ax, 80 ; Calculate the offset
mul y
mov cx, x
mov di, cx
and cl, 7 ; CL = shift value
mov shift, cl
shr di, 3
add di, ax ; DI -> byte offset
mov start_di, di
mov ax, [si].e_height ; AX = height of the sprite in rows
mov number_of_rows, ax ; Save for later use
shl ax, 3 ; AX *= 8, to get size of 1 sprite plane
mov sprite_plane_size, ax
mov ax, number_of_rows ; Let's see if it goes too low
add ax, y
sub ax, BOTTOM_LINE
jbe @F ; Skip if it doesn't
sub number_of_rows, ax ; Update the number of rows
@@: add si, E_BODY ; SI -> start of sprite body
mov start_si, si
cmp x, 640-32 ; See if we are going to be right
ja short_mask ; Skip if no mask
cmp shift, 0
jnz shifted
mov bl, NUMBER_OF_PLANES
next_plane:
dec bl
mov dx, GRAPHICS_12 ; Talk to EGA control logic
mov al, 4
mov ah, bl
out dx, ax
mov dx, SEQUENCE_REG ; Set up the ports for writing as well
mov ax, 100h + MAP_MASK_REG
mov cl, bl
shl ah, cl
out dx, ax
mov cx, number_of_rows
@@: mov ax, es:[di] ; Get the background dword
mov dx, es:[di+2]
and ax, [si] ; Do the mask dword
and dx, [si+4]
or ax, [si+2] ; Bring on the sprite
or dx, [si+6]
mov es:[di], ax ; Replace with new graphic dword
mov es:[di+2], dx
add si, 8 ; Next row stuff
add di, 80
loop @B
mov di, start_di
mov si, start_si
add si, sprite_plane_size
mov start_si, si
or bl, bl
jnz next_plane
jmp done
short_mask:
jmp masked
shifted:
mov plane_number, NUMBER_OF_PLANES-1
mov cl, shift
mov bh, -1
shr bh, cl
next_shift_plane:
mov dx, GRAPHICS_12 ; Talk to EGA control logic
mov al, 4
mov ah, plane_number
out dx, ax
mov dx, SEQUENCE_REG ; Set up the ports for writing as well
mov ax, 100h + MAP_MASK_REG
mov cl, plane_number
shl ah, cl
out dx, ax
mov ch, byte ptr number_of_rows
mov cl, shift
@@: lodsw ; Get the sprite mask
xchg ah, al ; Switch them around
ror ax, cl
mov bl, ah ; Top CL bits are ones to mask third byte
not bh ; BH = ~BH
and bl, bh
or ah, bh ; Now set the top ones of source byte
mov dx, es:[di] ; Get the first destination word
xchg ah, al ; Re-order the mask bytes
and dx, ax ; Mask it
lodsw ; Get the sprite
xchg ah, al
ror ax, cl
or dh, al ; OR DH w/ old AH
mov al, ah ; Save the upper bits
and ah, bh ; Mask off other bits
not bh ; BH = BH
and al, bh ; Cut out the rotunds
or dl, al ; OR least sig. bytes
mov es:[di], dx ; Save that first word, whew!
mov dl, ah ; DL = pushed up sprite bits
lodsw ; Get the next sprite mask
xchg ah, al ; Switch them around
ror ax, cl
mov dh, ah ; DH = MS shifted mask bits
and ah, bh ; Get rid of shifted bits
or ah, bl ; OR with shifted mask from previous byte
or dh, bh ; Add on the mask
xchg ah, al ; AH:AL back to normal
and es:[di+4], dh ; Easy way to get rid of DH
mov bl, dl ; BL = previous sprite bits
mov dx, es:[di+2] ; Get the destination word
and dx, ax ; Mask it
lodsw ; Get the sprite
xchg ah, al
ror ax, cl
or dh, al ; OR DH w/ old AH
mov al, ah ; Save the upper bits
not bh ; BH = ~BH
and ah, bh ; Mask off other bits
not bh ; BH = BH
and al, bh ; Cut out the rotunds
or al, bl
or dl, al ; OR least sig. bytes
mov es:[di+2], dx ; Save that first word, whew!
or es:[di+4], ah
add di, 80
dec ch
jnz @B
mov di, start_di
mov si, start_si
add si, sprite_plane_size
mov start_si, si
sub plane_number, 1
jc @F
jmp next_shift_plane
@@: jmp done
masked:
xor ax, ax ; Set up masks and ORs
mov mask_lsw, ax
mov mask_msw, ax
dec ax
mov or_lsw, ax
mov or_msw, ax
cmp x, 640-24 ; See if we have masked it already
jc @F ; Skip if we have
mov mask_msw, 0ff00h
mov or_msw, 0ffh
cmp x, 640-16
jc @F
mov mask_msw, -1 ; Make sure nothing gets masked
mov or_msw, 0
cmp x, 640-8 ; See if that's all
jc @F
mov mask_lsw, 0ff00h
mov or_lsw, 0ffh
@@: mov plane_number, NUMBER_OF_PLANES-1
next_mask_plane:
mov dx, GRAPHICS_12 ; Talk to EGA control logic
mov al, 4
mov ah, plane_number
out dx, ax
mov dx, SEQUENCE_REG ; Set up the ports for writing as well
mov ax, 100h + MAP_MASK_REG
mov cl, plane_number
shl ah, cl
out dx, ax
mov cx, number_of_rows
@@: mov ax, [si] ; Get the first mask
mov dx, [si+4] ; Get the second mask
or ax, mask_lsw
or dx, mask_msw
and ax, es:[di] ; Get the background dword
and dx, es:[di+2]
mov bx, or_lsw ; Get the OR lsw
and bx, [si+2] ; Bring on the sprite
or ax, bx
mov bx, or_msw
and bx, [si+6]
or dx, bx
mov es:[di], ax ; Replace with new graphic dword
mov es:[di+2], dx
add si, 8 ; Next row stuff
add di, 80
loop @B
mov di, start_di
mov si, start_si
add si, sprite_plane_size
mov start_si, si
sub plane_number, 1
jnc next_mask_plane
jmp done
done: pop ds
ret
put_sprite endp
; EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
;
; void EGA_CLEAR_AREA( ui X1, ui Y1, ui X2, ui Y2 )
; Clears an area on the EGA display to black
; Given:
; X1,Y1 = X,Y pixel coordinates of the upper left corner
; X2,Y2 = X,Y pixel coordinates of the lower right corner
; Returns:
; Rectangular area from X1,Y1 to X2,Y2 (inclusive) cleared to black
public ega_clear_area
ega_clear_area proc uses ds si di, x1:word, y1:word, x2:word, y2:word
local height:word
local di_start:word
local word_columns:word
local di_offset:word
cld
cmp x1, 640 ; See if too far to the right
jc @F
mov x1, 0
@@: cmp x2, 640
jc @F
mov x2, 639
@@: mov ax, y2 ; Check out number of rows
sub ax, y1
jnc @F ; See if jerk put them in backwards
neg ax
@@: inc ax
mov bx, ax ; BX = number of rows
mov height, ax
mov ax, y1 ; Check our starting offset
mov cx, 80
mov di_offset, cx
mul cx
mov di, ax
mov ax, x1 ; See where we start
shr ax, 3
add di, ax
mov ax, x2
sub ax, x1
jnc @F
neg ax
@@: add ax, 16
shr ax, 4
jnz @F
jmp done
@@: mov word_columns, ax
add ax, ax
sub di_offset, ax
mov es, ega_segment
mov dx, SEQUENCE_REG ; Set up the ports for writing
mov ax, 0f00h+MAP_MASK_REG
out dx, ax
mov dx, di_offset
xor ax, ax
mov di_start, di
@@: mov cx, word_columns
rep stosw
add di, dx
dec bx
jnz @B
done: ret
ega_clear_area endp
; EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
;
; void EGA_INSTALL()
; Installs IRQ-2 vectors for EGA card
; Given:
; nothing
; Returns:
; -1 if neither EGA or VGA else
; EGA interrupt vector installed and enabled
public ega_install
ega_install proc
mov ax, 40h ; ES -> video BIOS data area
mov es, ax
mov dx, es:[EGA_ADDRESS] ; DX -> CRTC Address port
mov ega_base_port, dx ; Save the port address
mov ax, 1a00h ; Read display combination
int 10h
cmp al, 1ah ; See if EGA
jne ega_adaptor
cmp bl, 7 ; See if VGA
je vga_adaptor
cmp bl, 8
je vga_adaptor
error_out:
mov ax, -1
jmp short done
ega_adaptor:
mov al, es:[CRT_MODE] ; AL = video BIOS mode number
mov bx, offset EGA_settings
xlat
jmp short @F
vga_adaptor:
mov al, v_retrace_reg ; AL = Vertical retrace register
out dx, al
inc dx
in al, dx
@@: mov v_retrace_value, al
mov done_page_flip, -1
mov do_page_flip, 0
xor ax, ax ; ES -> base page
mov es, ax
mov bx, 0ah*4 ; Vector for IRQ 2
mov dx, cs
mov ax, offset ega_interrupt
cli
xchg es:[bx], ax
xchg es:[bx+2], dx
mov old_vector, ax
mov old_vector+2, dx
in al, 21h ; Get present mask
mov old_irq_mask, al
and al, 11111011b
out 21h, al
mov dx, ega_base_port
mov ax, default_retrace
and ah, 11001111b
out dx, ax
jmp short $+2
or ah, 00010000b
out dx, ax
sti
done: ret
ega_install endp
; EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
;
; void EGA_RIP_OUT()
; Undoes all the interrupt processing for the EGA
; Given:
; nothing
; Returns:
; EGA interrupt vector removed
public ega_rip_out
ega_rip_out proc uses es
mov ax, old_vector ; See if installed
or ax, old_vector+2
jz done ; Return if not installed
xor ax, ax ; ES -> base page
mov es, ax
mov bx, 0ah*4 ; Vector for IRQ 2
mov ax, old_vector
mov dx, old_vector+2
mov old_vector, 0
mov old_vector+2, 0
cli
mov es:[bx], ax
mov es:[bx+2], dx
in al, 21h
mov ah, old_irq_mask ; Restore old interrupt mask
and ah, 1 shl 2
and al, 11111011b
or al, ah
out 21h, al
mov dx, 3d4h
mov ax, 2b11h
out dx, ax
sti
done: ret
ega_rip_out endp
; EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
;
; void EGA_INTERRUPT( void )
; Handles all the interrupt processing for the EGA controller
; Given:
; This is run at every IRQ-2 spike (ie. vertical retrace)
; Returns:
; If FLIP_TURN is brought to zero, EGA visual pages are swapped
; If swap is made, DO_PAGE_FLIP set to 0, DONE_PAGE_FLIP set to -1
ega_interrupt proc far
push ax
push dx
push ds
mov ax, @DATA
mov ds, ax
mov dx, 3c2h ; DX -> I/O port for input status
in al, dx
test al, 1 shl 7
jnz @F ; Interrupt is ours
pushf
call dword ptr [old_vector]
jmp done
@@: mov dx, ega_base_port ; DX -> EGA/VGA register port
in al, dx
push ax
mov ax, default_retrace
and ah, 11101111b
out dx, ax
jmp short $+2
mov al, EOI
out EOI_PORT, al
jmp short $+2
sti
dec flip_turn
jnz @F
mov flip_turn, 4
cmp do_page_flip, 0 ; See if we need to do this
jz @F
mov al, 0ch ; Select register for page MSB
mov ah, byte ptr ega_segment+1
shl ah, 4
out dx, ax ; Output the most significant byte
xor ega_segment, 800h ; Swap the active page
jmp short $+2
mov do_page_flip, 0
mov done_page_flip, -1
@@: cli
mov ax, default_retrace
and ah, 11011111b
or ah, 00010000b
out dx, ax
jmp short $+2
pop ax
out dx, al
done: pop ds
pop dx
pop ax
iret
ega_interrupt endp
end